package com.chensheng.controller;

import com.chensheng.dto.OrderDTO;
import com.chensheng.enums.ProductStatusEnum;
import com.chensheng.enums.ResultErrorEnum;
import com.chensheng.exception.SellException;
import com.chensheng.form.ProductForm;
import com.chensheng.model.ProductCategory;
import com.chensheng.model.ProductInfo;
import com.chensheng.service.ProductCategorySercvice;
import com.chensheng.service.ProductInfoService;
import com.chensheng.util.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.jnlp.IntegrationService;
import javax.validation.Valid;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:ChenSheng
 * @Description:
 * @Date: Created in 下午 14:12 2018/5/18 0018
 */
@Controller
@RequestMapping("/seller/product")
@Slf4j
public class SellerProductController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategorySercvice productCategorySercvice;


    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "5") Integer size,
                             Map<String, Object> map){
        Integer start = (page - 1) * size;
        Integer offset = size;
        List<ProductInfo> productInfoList = productInfoService.findAll(start, offset);
        Integer totalCount = productInfoService.count();
        totalCount = (totalCount / size) + 1;

        map.put("productInfoList", productInfoList);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("totalCount", totalCount);

        return new ModelAndView("product/list", map);
    }

    @RequestMapping("/off_sale")
    public ModelAndView offSale(@RequestParam(value = "productId") String productId,
                             Map<String, Object> map){
        try{
            productInfoService.off_sale(productId);
        }catch(SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/seller/product/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/seller/product/list");
        return new ModelAndView("common/success", map);
    }

    @RequestMapping("/on_sale")
    public ModelAndView onSale(@RequestParam(value = "productId") String productId,
                             Map<String, Object> map){
        try{
            productInfoService.on_sale(productId);
        }catch(SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/seller/product/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/seller/product/list");
        return new ModelAndView("common/success", map);
    }


    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId", required = false) String productId,
                              Map<String, Object> map){
        if(!StringUtils.isEmpty(productId)){
            ProductInfo productInfo = productInfoService.findOne(productId);
            map.put("productInfo", productInfo);
        }

        List<ProductCategory> productCategoryList = productCategorySercvice.findAll();
        map.put("categoryList", productCategoryList);
        return new ModelAndView("product/index", map);
    }

    @PostMapping("/save")
    public ModelAndView save(@Valid ProductForm form,
                             BindingResult bindingResult,
                             Map<String, Object> map){
        if(bindingResult.hasErrors()){
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/seller/product/index");
            return new ModelAndView("common/error", map);
        }

        try{
            if(StringUtils.isEmpty(form.getProductId())){
                // 插入一条数据
                ProductInfo productInfo = new ProductInfo();
                BeanUtils.copyProperties(form, productInfo);
                productInfo.setProductId(KeyUtil.genUniqueKey());
                Date date = new Date();
                productInfo.setCreateTime(date);
                productInfo.setUpdateTime(date);
                productInfo.setProductStatus(ProductStatusEnum.UP.getCode());

                productInfoService.save(productInfo);
            }else{
                // 更新数据
                ProductInfo productInfo = productInfoService.findOne(form.getProductId());
                BeanUtils.copyProperties(form, productInfo);

                Date date = new Date();
                productInfo.setUpdateTime(date);

                productInfoService.update(productInfo);
            }
        }catch(Exception e){
            map.put("msg", e.getMessage());
            map.put("url", "/seller/product/index");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/seller/product/list");
        return new ModelAndView("common/success");
    }

}
